package cc.ruih.meizanchen.mm;

import cc.ruih.meizanchen.R;
import cc.ruih.meizanchen.Setting;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 更新妈妈的个人信息
 * @author Ruih08
 *
 */
public class MMInfo extends Activity {

	EditText mmmz;
	EditText ycq;
	Button save;
	
	private int mYear;
	private int mMonth;
	private int mDay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mm_info);
		
		mmmz = (EditText) findViewById(R.id.mmmzUp);
		ycq = (EditText) findViewById(R.id.ycqUp);
		save = (Button) findViewById(R.id.save);
		
		// 从SharedPreferences中取值
		SharedPreferences sharedPreferences = getSharedPreferences("mminfo", Context.MODE_PRIVATE);
		mmmz.setText(sharedPreferences.getString("name", ""));
		ycq.setText(sharedPreferences.getString("ycq", ""));
		
		// 预产期修改
		ycq.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				formatDate(ycq.getText().toString());
				createTimePickerDialog().show();
			}
		});
		ycq.setInputType(InputType.TYPE_NULL);
		// 点击保存按钮
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mmmz.getText().toString() != null && mmmz.getText().toString() != "" && mmmz.getText().toString().length() > 0
						&& ycq.getText().toString() != null && ycq.getText().toString() != "" && ycq.getText().toString().length() > 0) {
					SharedPreferences sharedPreferences = getSharedPreferences("mminfo", Context.MODE_PRIVATE);
					Editor editor = sharedPreferences.edit();	//获取编辑器
					editor.putString("name", mmmz.getText().toString());
					editor.putString("ycq", ycq.getText().toString());
					editor.commit();
					Intent intent = new Intent();
					intent.setClass(MMInfo.this, Setting.class);	// 跳转到孕妈妈首页
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "妈妈的名字和预产期不能为空", Toast.LENGTH_LONG).show();
					return;
				}
			}
		});
	}
	
	// 格式化求出预产期
	public void formatDate(String str){
		String[] strs=str.split("年");
		mYear=Integer.parseInt(strs[0]);
		String[] strs2=strs[1].split("月");
		mMonth=Integer.parseInt(strs2[0])-1;
		String[] strs3=strs2[1].split("日");
		mDay=Integer.parseInt(strs3[0]);
	}
	
	//创建DatePickerDialog
	protected Dialog createTimePickerDialog() {
		return new DatePickerDialog(this,
                mDateSetListener, mYear, mMonth, mDay);
	}
	
	//DatePickerDialog上的监听
	private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int month,
                    int dayOfMonth) {
            	ycq.setText(new StringBuilder()
        	                    .append(year).append("年")
        	                    .append(month + 1).append("月")
        	                    .append(dayOfMonth).append("日"));
            	mYear=year;
            	mMonth=month;
            	mDay=dayOfMonth;
            }
        };
}
